/**
 * created by 江心才子, 2019/8/6
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.home.leaderboard

import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.TYPE_HEADER
import com.wyl.base.TYPE_NORMAL
import com.wyl.home.R
import com.wyl.home.databinding.RolelistActivityBinding
import com.wyl.home.databinding.RolelistItemBinding
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.autoWired
import io.ditclear.bindingadapter.BindingViewHolder
import io.ditclear.bindingadapter.ItemDecorator
import io.ditclear.bindingadapter.MultiTypeAdapter
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * 项目名称：ShortVideo
 * 创建人：江心才子
 * 创建时间：2019-08-06 10:28
 * 内容描述：
 * 修改说明：
 */
class RolelistActivity : BindingActivity<RolelistActivityBinding>(), ItemDecorator {
    private val viewModel: RolelistViewModel by viewModel()

    override fun getLayoutId(): Int = R.layout.rolelist_activity

    override fun initView() {
        binding.vm = viewModel

        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }

        binding.include.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }

    }

    private val mAdapter by lazy {
        MultiTypeAdapter(
            this,
            viewModel.dataSource,
            object : MultiTypeAdapter.MultiViewTyper {
                override fun getViewType(item: Any): Int = when (item) {
                    is RolelistViewModel -> TYPE_HEADER
                    else -> TYPE_NORMAL
                }
            }).apply {
            addViewTypeToLayoutMap(TYPE_HEADER, R.layout.rolelist_item_header)
            addViewTypeToLayoutMap(TYPE_NORMAL, R.layout.rolelist_item)
            itemDecorator = this@RolelistActivity
//            itemPresenter = this@VideoDetailActivity
        }
    }

    override fun loadData() {
        viewModel.id = autoWired("data", "")
        viewModel.refresh()
    }

    override fun decorator(holder: BindingViewHolder<ViewDataBinding>, position: Int, viewType: Int) {
        if (position > 0) {
            val itemBinding = holder.binding as RolelistItemBinding
            itemBinding.text.text = (position + 3).toString()
        }
    }

}